//C09:Stash4.h
// Inline functions
#ifndef STASH4_H_
#define STASH4_H_
#include "../require.h"

class Stash
{
    int size;    // Size of each space
    int quantity;    // Number of storage spaces
    int next;    // Next empty space
    // Dynamically allocated array of bytes:
    unsigned char * storage;
    void inflate(int increase);
    public:
        Stash(int sz): size(sz), quantity(0),
        next(0), storage(0){}
        Stash(int sz, int initQuantity): size(sz),
        quantity(0), next(0), storage(0)
        {
            inflate(initQuantity);
        }
        Stash::~Stash()
        {
            if(storage != 0)
            {
                delete []storage;
            }
        }
        int add(void* element);
        void * fetch(int index) const 
        {
            require(0 <= index, "Stash::fetch (-) index");
            if(index >= next)
            {
                return 0; // To indicate the end
            }
            // Produce pointer to desired element:
            return &(storage[index * size]);
        }

        int count() const { return next; }
};
#endif